//
//  MCLoginDynamicViewController.swift
//  StoreManage
//
//  Created by MC on 2018/8/29.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCLoginDynamicViewController: MCViewController {
    

    public var fatherVC : UIViewController?

    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        baseSetting()
        
        initUI()
    }
    
    // MARK: - System Delegate
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    // MARK: - Custom Delegate
    
    // MARK: - Action
    @objc func codeButtonClicked() {
        
        guard phoneTextField.text?.count == 11 else {
            MCAlterController_oneOptions(vc: self, message: "请填写正确的手机号", cancle: { })
            return
        }
        
        self.codeTextField.cutDownButton.canRequest = true
        
        let para = [
            "userName" : phoneTextField.text!,
            "codeType" : 0
            ] as [String : Any]
        
        MCNetwork_post(url: JAVA_sendCode, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in
            
            self.codeTextField.cutDownButton.isRequestSuccess = true
            
            MCHUD_text(message: "验证码发送成功", view: self.view, delay: 1.5)
            
        }, dislike: { (response) in }) { (error) in }
    }
    
    
    @objc func loginButtonClicked() {
        
        
        guard (phoneTextField.text?.count == 11 && codeTextField.text.count != 0) else {
            MCAlterController_oneOptions(vc: self, message: "账号或者验证码不正确", cancle: { })
            return
        }
        
        let para = [
            "userName" : phoneTextField.text ?? "",
            "code"     : codeTextField.text
            ] as [String : Any]
        
        
        
        MCNetwork_post(url: JAVA_dynamicRegister, para: para as [String : AnyObject], vc: self,hudSuperView:self.view, isHiddenTabbar: true, success: { (response) in
            
            let dataDcit = response["data"] as? [String:Any] ?? [:]
         
            
            MCHUD_text(message: "登录成功", view: self.view, delay: 1)
            
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1, execute: {
                let vc = MCChangeStoreViewController()
                vc.isLoginVCJump = true
                vc.userDict = dataDcit
                let nav = MCNavigationController.init(rootViewController: vc)
                self.fatherVC!.present(nav, animated: true, completion: nil)
                
            })
        }, dislike: { (response) in }) { (error) in }
    }
    
    
    // MARK: - Method
    func baseSetting() { }
    
    
    func initUI() {
        
        view.addSubview(phoneTextField)
        phoneTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(25)
            make.height.equalTo(40)
        }
        
        view.addSubview(codeTextField)
        codeTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(phoneTextField.snp.bottom).offset(10)
            make.height.equalTo(40)
        }
        
        
        view.addSubview(loginButton)
        loginButton.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(codeTextField.snp.bottom).offset(40)
            make.height.equalTo(40)
        }
        
        
        
        
        view.addSubview(promptImageView)
        promptImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view).offset(MCAdaptationModel(-30))
        }
        
        view.addSubview(logoImageView)
        logoImageView.snp.remakeConstraints { (make) ->Void in
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize.init(width: 50, height: 50))
            make.bottom.equalTo(promptImageView.snp.top).offset(MCAdaptationModel(-20))
        }
    }
    
    // MARK: - Setter & Getter
    lazy var logoImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Logo")
        return imageView
    }()
    
    lazy var promptImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = #imageLiteral(resourceName: "Login_prompt")
        return imageView
    }()
    
    lazy var phoneTextField: MCTextFiled = {
        let textField = MCTextFiled()
        textField.placeholder = "请输入手机号码"
        textField.font = MCFont(14)
        textField.keyboardType = UIKeyboardType.numberPad
        return textField
    }()
    
    lazy var codeTextField: MCVerificationCodeTextField = {
        let tf = MCVerificationCodeTextField()
        weak var weakSelf = self        
        tf.cutDownButton.clickedClosure = { () -> Void in
            weakSelf?.codeButtonClicked()
        }
        
        return tf
    }()
    
    
    lazy var loginButton: UIButton = {
        let button = UIButton.init(type: UIButtonType.custom)
        button.titleLabel?.font = MCFont(14)
        button.backgroundColor = UIColor.MCMain
        button.layer.cornerRadius = 18
        button.layer.masksToBounds = true
        button.setTitle("登录", for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        button.addTarget(self, action: #selector(loginButtonClicked), for: UIControlEvents.touchUpInside)
        return button
    }()
    
}
